//
//  SEGCommonDoorConfigCollectionViewCell.m
//  SEGIntelligentHardware
//
//  Created by Clavis on 2019/5/17.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "SEGCommonDoorConfigCollectionViewCell.h"

@interface SEGCommonDoorConfigCollectionViewCell ()

@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) CAShapeLayer *borderLayer;

@end

@implementation SEGCommonDoorConfigCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    self.container = [[UIView alloc] init];
    self.container.backgroundColor = [UIColor colorWithHexString:@"#FAF9F9"];
    [self addSubview:self.container];
    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
        make.height.mas_equalTo(38);
    }];
    
    self.icon = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = UIColor.clearColor;
        [self.container addSubview:imageView];
        imageView;
    });
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-9);
        make.width.height.mas_equalTo(12);
        make.centerY.mas_offset(0);
    }];
    
    self.nameLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [self.container addSubview:label];
        label.textColor = kColor_1;
        label.font = kFont_13;
        label;
    });
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.mas_offset(9);
        make.right.equalTo(self.icon.mas_left).offset(-4);
    }];
}

- (void)updateCellWithModel:(SEGNewMyDoorListModel *)model displayType:(SEGCommonDoorConfigCellDisplayType)displayType {
    switch (displayType) {
        case SEGCommonDoorConfigCellDisplayTypeNone: {
            // 没有门禁数据的时候，展示边框
            [self.layer addSublayer:self.borderLayer];
            self.container.hidden = YES;
            break;
        }
        case SEGCommonDoorConfigCellDisplayTypeCanAdd: {
            self.container.hidden = NO;
            self.icon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_set_add")];
            [self.borderLayer removeFromSuperlayer];
            break;
        }
        case SEGCommonDoorConfigCellDisplayTypeCanDelete: {
            self.container.hidden = NO;
            self.icon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_set_del")];
            [self.borderLayer removeFromSuperlayer];
            break;
        }
        case SEGCommonDoorConfigCellDisplayTypeDidSelect: {
            self.container.hidden = NO;
            self.icon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_set_select")];
            [self.borderLayer removeFromSuperlayer];
            break;
        }
        default:
            break;
    }
    self.nameLabel.text = model.name;
}

- (CAShapeLayer *)borderLayer {
    if (!_borderLayer) {
        _borderLayer = ({
            CAShapeLayer *border = [CAShapeLayer layer];
            //虚线的颜色
            border.strokeColor = kColor_L2.CGColor;
            //填充的颜色
            border.fillColor = [UIColor clearColor].CGColor;
            //设置路径
            border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
            border.frame = self.bounds;
            //虚线的宽度
            border.lineWidth = 1.f;
            //设置线条的样式
            //    border.lineCap = @"square";
            //虚线的间隔
            border.lineDashPattern = @[@4, @2];
            border;
        });
    }
    return _borderLayer;
}

@end
